from flask import Flask, render_template, request, redirect
import json
app = Flask(__name__)

from service import searchLogService

#加载数据库
def load_data():
    searchLogService.sogou_data = searchLogService.load_data('../static/test.useful.new')
'''
获取一天的日志(不重要，不看)
'''
@app.route('/log/day', methods=['GET'])
def get_oneday_log(date='2011_12_30'):
    # 加载日志数据
    # searchLogService.sogou_data = searchLogService.load_data('../static/history_'+date+'.csv')
    # 测试用
    searchLogService.sogou_data = searchLogService.load_data('../static/test.useful.new')
    # 小时活量统计【不同时段搜索关键词的数量】[折线图柱状图]
    searchLogService.get_time_visit()
    # 关键词热度统计【一天内搜索频率较高关键词】【水平柱状图】
    searchLogService.get_day_word()
    # 获得用户个人报表图
    searchLogService.get_users_word_time()
    searchLogService.get_personal_log('0d56cf20da81670adf923373d50b7dbc')
    return '修改个人信息'

'''
获取时间段数据
{value: 1, name: '深夜'}
'''
@app.route('/log/quantum', methods=['GET'])
def get_quantum():
    if searchLogService.sogou_data is None:
        load_data()
    return searchLogService.get_quantum()

'''
获取时间数据
{time:[],visit_count:[]}
'''
@app.route('/log/time_visit', methods=['GET'])
def get_time_visit():
    if searchLogService.sogou_data is None:
        load_data()
    return searchLogService.get_time_visit()

'''
获取统计总数据
{
search_count:5555,
visit_count:1332,
hot_word:'新闻',
ex_word:44
}
'''
@app.route('/log/reports', methods=['GET'])
def get_reports():
    if searchLogService.sogou_data is None:
        load_data()
    searchLogService.get_reports()


'''
获取异常词列表
{'id': 100, '关键词': 'aaa', '罚分': 107}
'''
@app.route('/log/ex_words', methods=['GET'])
def get_ex_word_list():
    if searchLogService.sogou_data is None:
        load_data()
    searchLogService.get_ex_word_list()

'''
异常词调用，查询相关异常信息
'''
def get_ex_word_detail(key_id):
    searchLogService.get_personal_log(key_id)

'''
获取个人用户的搜索信息
（最近一天的高频访问词列表、访问频繁时段）
'''
def get_personal_log(user_id):
    if searchLogService.sogou_data is None:
        load_data()
    searchLogService.get_personal_log(user_id)

# 接收前端的操作日志
def get_front_log():
    # TODO
    print("接收前端的操作日志")
if __name__ == '__main__':
    app.run()
    # get_quantum()
    # print(get_time_visit()
    # get_ex_word_list()
    # get_personal_log()
    # get_front_log()

